package com.ruoyi.report.forms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.report.forms.domain.ReportFormsTemplate;
import com.ruoyi.report.forms.domain.UpdateReport;
import com.ruoyi.report.forms.service.IReportFormsTemplateService;
import com.ruoyi.report.forms.util.DataUpdate;
import com.ruoyi.report.forms.util.ExcelDataSelect;
import com.ruoyi.report.forms.util.ExcelHelper;
import com.ruoyi.report.forms.util.TableDataSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

import static com.ruoyi.common.core.web.domain.AjaxResult.success;

@RestController
@RequestMapping("/table")
public class ReportFormsTableController {

    @Autowired
    private IReportFormsTemplateService reportFormsTemplateService;

    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, ReportFormsTemplate reportFormsTemplate) {
        ReportFormsTemplate template = reportFormsTemplateService.selectReportFormsTemplateByTemplateId(reportFormsTemplate.getTemplateId());
        ExcelHelper.ExportExcel(response, template);
    }

    @PostMapping("/importExcel")
    public HashMap<String, Object> importExcel(@RequestParam("file") MultipartFile file) {
        return ExcelHelper.ImportExcel(file);
    }

    @PostMapping("/getExcelByDay")
    public void getExcelByDay(HttpServletResponse response, HttpServletRequest request) {

        // 获取参数
        String id = request.getParameter("id");
        String date = request.getParameter("date");
        String month = request.getParameter("month");

        // 从数据库查询模板数据
        ReportFormsTemplate template = reportFormsTemplateService.selectReportFormsTemplateByTemplateId(id);

        // 生成excel表格
        ExcelHelper.ExportSpreadsheet(response, template, date, month);
    }

    @GetMapping("/getReportByDay")
    public AjaxResult getReportByDay(HttpServletRequest request) {

        // 获取参数
        String id = request.getParameter("id");
        String date = request.getParameter("date");
        String month = request.getParameter("month");

        // 从数据库查询模板数据
        ReportFormsTemplate template = reportFormsTemplateService.selectReportFormsTemplateByTemplateId(id);

        JSONObject config = JSON.parseObject(template.getTemplateConfig());

        // 从数据库查询历史数据
        TableDataSelect tableDataSelect = new TableDataSelect(config.getJSONArray("bindata"), date, month);

        // 替换模板中的实时数据
        tableDataSelect.WriteTableData(config.getJSONObject("rows"));

        // 将替换完的实时数据内容保存起来
        template.setTemplateConfig(JSON.toJSONString(config));

        return success(template);
    }

    @PostMapping("/updateReportValue")
    public void updateReportValue(@RequestBody UpdateReport updateReport) {
        DataUpdate.Update(updateReport);
    }
}
